/*
 * Copyright (C) 2013～2023 上海颐凡软件科技有限公司
 * Yfann Software Technology (Shanghai) Co.,LTD
 * All Rights Reserved.
 * 公司网址: www.yfann.com
 * 365IT教育网，成就您优秀架构师之梦！
 * 平台网址: www.365itedu.com
 */
package com.yfann.web.base.sys.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.MDC;

import com.yfann.sword.web.fwk.bean.UIOBean;
import com.yfann.sword.web.fwk.consts.FrameworkConst;

/**
 * 用户访问URL权限控制类。
 * 
 * <pre>
 * 修改履历
 * -----------------------------------------------------------------------------
 *         VERSION       DATE            BY       CHANGE/COMMENT
 * -----------------------------------------------------------------------------
 *          1.0         2013/10/11      颜廷吉            create
 * -----------------------------------------------------------------------------
 * </pre>
 */
public class MDCLogFilter implements Filter {
    /**
     * 过滤器上下文
     */
    private FilterConfig filterConfig;

    /** 程序ID */
    private static final int PID = java.util.Calendar.getInstance().hashCode();

    /**
     * Filter初始化。
     * @param filterConfig 过滤器
     */
    @Override
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    /**
     * 日志过滤器处理。<br>
     * @param request 请求
     * @param response 响应
     * @param chain 过滤器链
     * @throws ServletException IOException
     * @throws IOException IOException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (filterConfig == null) {
            return;
        }

        if (request instanceof HttpServletRequest) {
            HttpServletRequest servletRequest = (HttpServletRequest) request;
            String remoteAddr = servletRequest.getRemoteAddr();
            String hostName = servletRequest.getRemoteHost();

            MDC.put("ipAddr", remoteAddr);
            MDC.put("hostName", hostName);
            HttpSession session = servletRequest.getSession();
            // 用户信息设定
            UIOBean uio = (UIOBean) session.getAttribute(FrameworkConst.USER_INFO_OBJECT_KEY);
            if (uio != null) {
                MDC.put("userId", uio.getUserId());
            }
            // sessionId设定
            String sessionId = session.getId();
            if (sessionId == null) {
                sessionId = "noSession";
            }
            MDC.put("sessionId", sessionId);
            MDC.put("PID", PID);
            MDC.put("ThreadId", String.format("%X", Thread.currentThread().getId()));
        }

        chain.doFilter(request, response);

        MDC.remove("ipAddr");
        MDC.remove("hostName");
        MDC.remove("userId");
        MDC.remove("sessionId");
        MDC.remove("PID");
        MDC.remove("ThreadId");
    }

    /**
     * 销毁日志过滤器。
     */
    @Override
    public void destroy() {
        this.filterConfig = null;
    }
}
